﻿using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpriteSheetsCreator {
    public class TextUtil {
		#region Date time methods
        public static string parseAndGetFormattedDateTime(string str) {
            if (str == null || str == "") {
                return "";
            }
            try {
                DateTime argDate = DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture);
                return argDate.ToString("dd/MM/yyyy HH:mm");
            }
            catch { return ""; }
        }

        public static string parseAndGetFormattedTime(string str) {
            if (str == null || str == "") {
                return "";
            }
            try {
                DateTime argDate = DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture);
                return argDate.ToString("HH:mm");
            }
            catch { return ""; }
        }

        public static int parseAndGetDelayToDate(string str) {
            DateTime argDate = DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture);
            return (argDate - DateTime.Now).Minutes;
        }

        public static DateTime parseDate(string str) {
            return DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture);
        }

        public static string getDateTimeString(DateTime dt) {
            return dt.ToString("yyyyMMddHHmm");
        }
		#endregion

        #region List methods
        /// <summary>
        /// get string list from splitted string
        /// </summary>
        /// <returns></returns>
        public static List<string> getListFromString(string inStr, char[] separator) {
            List<string> result = new List<string>();
            if (inStr.isNullOrEmpty()) {
                return result;
            }
            string[] array = inStr.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            foreach (string ss in array) {
                result.Add(ss);
            }
            return result;
        }

        /// <summary>
        /// get string list from splitted string
        /// </summary>
        /// <returns></returns>
        public static List<string> getListFromString(string inStr, char separator) {
            return getListFromString(inStr, new char[] { separator });
        }

        /// <summary>
        /// get a string list, remove duplicate elements then return deduplicated string list
        /// </summary>
        /// <returns></returns>
        public static List<string> removeDuplicates(List<string> inList) {
            List<string> result = new List<string>();
            foreach (string id_str in inList) {
                if (!result.Contains(id_str)) {
                    result.Add(id_str);
                }

            }
            return result;
        }

        /// <summary>
        /// get string from string list
        /// </summary>
        /// <returns></returns>
        public static string getStringFromList(List<string> inList, char separator) {
            string result = "";
            string listSeparator = "";
            foreach (string val in inList) {
                result += separator + val;
                listSeparator = separator.ToString();
            }
            return result;
        }
        #endregion
    }
}
